package com.yyw.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yyw.bean.User;
import com.yyw.dao.CollectDao;
import com.yyw.util.MyContext;
import com.yyw.util.Result;
import com.yyw.util.ResultFactory;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@Api(tags = "收藏API")
@RestController
@RequestMapping("/user")
@Slf4j
public class CollectController {

    @Autowired
    private CollectDao collectDao;

    @CrossOrigin
    @ApiOperation(value = "取消收藏", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "aid", value = "短文|分享文的ID", required = true) })
    @GetMapping("/deleteCollect")
    @Transactional
    public Result deleteCollect(@RequestParam("aid") Long aid) {

        if (aid == null)
            return ResultFactory.notice("数据不合法");

        User user = MyContext.getSessionUser();
        int i = collectDao.deleteCollect(user.getId(), aid);
        if (i > 0) {
            return ResultFactory.success("取消成功");
        } else {
            return ResultFactory.failure("取消失败");
        }
    }

    @CrossOrigin
    @ApiOperation(value = "添加收藏", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "type", value = "类型，1短文，2分享文", required = true),
            @ApiImplicitParam(name = "aid", value = "短文|分享文的ID", required = true) })
    @GetMapping("/addCollect")
    @Transactional
    public Result addCollect(@RequestParam("type") Integer type, @RequestParam("aid") Long aid) {

        if (type == null || !(type == 1 || type == 2))
            return ResultFactory.notice("数据不合法");

        User user = MyContext.getSessionUser();
        int i = collectDao.saveCollect(user.getId(), type, aid);
        if (i > 0) {
            return ResultFactory.success("收藏成功");
        } else {
            return ResultFactory.failure("收藏失败");
        }
    }

    @CrossOrigin
    @ApiOperation(value = "分页获取自己的收藏", httpMethod = "GET")
    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNo", value = "当前页数", required = false),
            @ApiImplicitParam(name = "pageNum", value = "每页数量", required = false),
            @ApiImplicitParam(name = "type", value = "收藏类型，默认1(短文)，2分享文", required = false) })
    @GetMapping("/getCollects")
    public Result getCollects(@RequestParam(value = "type", required = false, defaultValue = "1") Integer type,
            @RequestParam(value = "pageNo", defaultValue = "1", required = false) Integer pageNo,
            @RequestParam(value = "pageNum", defaultValue = "10", required = false) Integer pageNum) {
        User user = MyContext.getSessionUser();

        List list = null;
        PageHelper.startPage(pageNo, pageNum);
        if (type != null && type.equals(2)) {
            list = collectDao.getShareList(user.getId());
        } else {
            list = collectDao.getTractateList(user.getId());
        }

        PageInfo pageInfo = new PageInfo<>(list);

        return ResultFactory.success(pageInfo);
    }
}
